﻿initUsers();
function initUsers() {
  $.ajax({
    url: '/users.do',
    method: 'GET',
    success: function (result) {
      if (result && result.length > 0) {
        for (var i = 0; i < result.length; i++) {
          var val = result[i];
          $(`<tr>
                <td>${val.id}</td>
                <td>${val.username}</td>
                <td>${val.phone}</td>
                <td>${val.state == 1 ? '正常' : '冻结'}</td>
                <td>
                  <button class="am-btn am-btn-success resetPass" myId="${val.id}">重置密码</button>
                  ${val.state == 1 ? `<button class="am-btn am-btn-danger state_change" myId="${val.id}">冻结</button>` : `<button class="am-btn am-btn-primary state_change" myId="${val.id}">解冻</button>`}
                </td>
            </tr>`).appendTo('tbody');
        }
      }
    }
  })
}

//重置密码
$(document).on('click', ".resetPass", function () {
  let id = $(this).attr('myId');
  swal({
    title: '请输入新密码',
    input: 'password',
    allowOutsideClick: false,
    showCancelButton: true,
    inputValidator: function (value) {
      return new Promise(function (resolve, reject) {
        if (value) {
          resolve()
        } else {
          reject('密码不能为空')
        }
      })
    }
  }).then(function (result) {
    if (!result) { return; }
    //Ajax
    $.ajax({
      url: '/resetPass.do',
      method: 'POST',
      data: `id=${id}&password=${result}`,
      success: function (result) {
        if (result === 'success') {
          swal('修改成功', '密码重置成功', 'success');
        } else {
          swal('修改失败', '密码重置失败', 'error');
        }
      }
    })

  })

})

//状态改变
$(document).on('click', '.state_change', function () {
  let txt = $(this).html();
  let id = $(this).attr('myId');
  let state = txt == '冻结' ? 0 : 1;
  swal({
    title: '确定要' + txt + '吗？',
    type: 'warning',
    showCancelButton: true,
    allowOutsideClick: false,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: '确定',
  }).then(function (result) {
    if (!result) return;
    //Ajax
    $.ajax({
      url: '/change_state.do',
      method: 'POST',
      data: `id=${id}&state=${state}`,
      success: function (result) {
        if (result == 'success') {
          location.reload();
        } else {
          swal(txt + '失败', '后台繁忙，请稍后再试', 'error');
        }
      }
    })
  })
})

//添加用户
$("#addBtn").click(function () {
  swal({
    title: '新增用户',
    html: `
        <input id="add_username" class="swal2-input" placeholder="请输入账号" autofocus/>
        <input id="add_pass" class="swal2-input" placeholder="请输入密码" type="password"/>
        <input id="add_phone" class="swal2-input" placeholder="请输入手机号码" type="number"/>
      `,
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    cancelButtonText: '取消',
    confirmButtonText: '确定',
    preConfirm: function () {
      return new Promise(function (resolve) {
        //Ajax
        $.post('/addUser.do', {
          username: $("#add_username").val(),
          password: $("#add_pass").val(),
          phone: $("#add_phone").val()
        }, function (result) {
          resolve(result);
        })
      })
    }
  }).then(function (result) {
    if (!result) return;
    if (result == 'success') {
      location.reload();
    } else {
      swal('添加失败', '新用户已添加失败', 'error');
    }
  }).catch(swal.noop)
})